package com.hoperun.product.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.hoperun.distributecloth.R;

import com.hoperun.product.entity.ClothEntity;

public class ClothListAdapter extends BaseAdapter {
	private Context mContext;
	private onDeleteClickListener mOnDeleteClickListener;
	private List<ClothEntity> mClothList = new ArrayList<ClothEntity>();

	public ClothListAdapter(Context context, List<ClothEntity> objects) {
		super();
		mContext = context;

		// 这里集合用=号赋值，改变内部productlist 外部list一样会变
		mClothList = objects;

	}

	@Override
	public int getCount() {

		return mClothList.size();
	}

	@Override
	public Object getItem(int position) {
		return mClothList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
		final ClothEntity cloth;
		cloth = (ClothEntity) getItem(position);
		View view;
		final ViewHolder viewholder;
		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.item_cloth_layout, parent, false);
			viewholder = new ViewHolder(view);
			view.setTag(viewholder);
		} else {
			view = convertView;
			viewholder = (ViewHolder) view.getTag();
		}

		viewholder.clothTktNoTv.setText(cloth.CLOTHTKTNO);
		viewholder.weightTv.setText(cloth.FINISHWEIGHT);

		viewholder.deleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// mClothList.remove(position);
				// ClothListAdapter.this.notifyDataSetChanged();
				if (mOnDeleteClickListener!=null) {
					mOnDeleteClickListener.onClick(position);
				}
				
			}
		});

		return view;

	}

	/**
	 * 设置删除接口
	 * 
	 * @param onDeleteClickListener
	 */
	public void setOnDeleteClickListener(onDeleteClickListener onDeleteClickListener) {
		mOnDeleteClickListener = onDeleteClickListener;
	}

	/**
	 * 删除接口
	 * 
	 * @author zhu_yu
	 * 
	 */
	public interface onDeleteClickListener {
		public void onClick(int position);
	}

	/**
	 * 
	 * 对控件实例进行缓存
	 * 
	 * @Description<功能详细描述>
	 * 
	 * @author zhu_yu
	 * @Version [版本号, 2017-5-26]
	 */
	class ViewHolder {
		int position;

		/** 三联单号 **/
		TextView clothTktNoTv;
		/** 重量 **/
		TextView weightTv;

		Button deleteBtn;

		public ViewHolder(View view) {
			clothTktNoTv = (TextView) view.findViewById(R.id.item_cloth_clothTktNoTv);

			weightTv = (TextView) view.findViewById(R.id.item_cloth_weightTv);

			deleteBtn = (Button) view.findViewById(R.id.item_cloth_deleteBtn);

		}

	}

}
